package com.itelite.controller;


import com.itelite.common.info.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 26-06-2021-19:26
 **/
@RestController
@Slf4j
@Api("登录模块")
public class LoginController {
    private final RequestCache requestCache = new HttpSessionRequestCache();
    /**
     * 未登录被拦截处理
     * @param request 请求
     * @param response 相应
     * @return 被拦截信息
     */
    @RequestMapping("/authentication/require")
    @ResponseStatus(code = HttpStatus.OK)
    public Result requireAuthentication(HttpServletRequest request, HttpServletResponse response) {
        SavedRequest savedRequest = requestCache.getRequest(request,response);
        if(savedRequest!=null){
            String targetUrl = savedRequest.getRedirectUrl();
            log.info("引发跳转的请求是:"+targetUrl);
        }
        return Result.failure(HttpStatus.UNAUTHORIZED,"访问的服务需要身份认证,请登陆");
    }
}
